@extends("base.base")
@include('Commodity.header')
@section('main')
    <link rel="stylesheet" href="{{asset(env('CDN_HOST')."/public/css/order/index.css")}}">
    <main id="main" class="main">
        <div class="order_div order_title">
            <div class="order_title_div order_title_left">
                <p class="order_title_p">下单时间: {{$parent_order["created_at"]}}</p>
                <p class="order_title_p">订单编号: {{$parent_order["parent_order_id"]}}</p>
            </div>
            <div class="order_title_div order_title_right">
                <i class="fa fa-trash-o close_order" data-state="{{$parent_order["state"]}}"
                   data-order-id="{{$parent_order["parent_order_id"]}}" aria-hidden="true"></i>
            </div>
            <div class="clear-both"></div>
        </div>
        <div class="order_div order_box">
            <div class="order_box_div box_title">
                <span class="order_info">包裹: {{$parent_order["parent_order_id"]}}</span>
                <span class="order_state">{{$parent_order["state_name"]}}</span>
            </div>
            <div class="order_box_div box_list">
                <div class="child_order">
                    <ul class="list-ul">
                        @foreach($parent_order["data"] as $k => $v)
                            <li class="list-li list-box" data-cart-id="{{$v["order_id"]}}">
                                <a href="/commodity/{{$v["commodity_id"]}}">
                                    <div class="box-img">
                                        <img src="{{asset($v["info_img_url"])}}" alt="">
                                    </div>
                                </a>
                                <div class="box-info">
                                    <div class="cnt">
                                        <p class="line1">
                                            <span class="line1-name">
                                                <span>{{$v["commodity_name"]}}</span>
                                            </span>
                                            <span class="line1-num">
                                                <span>x</span>
                                                <span class="commodity-number">{{$v["number"]}}</span>
                                            </span>
                                        </p>
                                        <p class="line2">{{$v["commodity_color_name"]}}
                                            ; {{$v["commodity_size_name"]}}</p>
                                        <div class="line3">
                                    <span class="line3-price">
                                        <span>¥{{$v["all_price"]}}</span>
                                        {{--<span class="commodity-price"--}}
                                        {{--data-unit-price="{{$v["unit_price"]}}"></span>--}}
                                    </span>
                                        </div>
                                    </div>
                                </div>
                            </li>
                        @endforeach
                    </ul>
                </div>
            </div>
            <div class="order_box_div box_play">
                <span class="order_state_name"
                      data-state="{{$parent_order["state"]}}"
                      data-order-id="{{$parent_order["parent_order_id"]}}">{{$parent_order["state_name_back"]}}</span>
                <div class="clear-both"></div>
            </div>
        </div>
        <div class="order_div order_beizhu">
            <div class="order_address">
                <p class="order_address_user">
                    <span class="address_user_name">{{$parent_order["user_address"]["get_name"]}}</span>
                    &emsp;&emsp;&emsp;&emsp;
                    <span class="address_phone">{{$parent_order["user_address"]["phone"]}}</span>
                </p>
                <p class="order_address_content">{{$parent_order["user_address"]["address"]}}</p>
            </div>
            <div class="order_price">
                <div class="order_price_top">
                    <table class="price_table">
                        <tr>
                            <td class="td-info">支付方式:</td>
                            <td class="td-price">储值卡</td>
                        </tr>
                        <tr>
                            <td class="td-info">商品合计:</td>
                            <td class="td-price">¥{{number_format($parent_order["all_price"],2)}}</td>
                        </tr>
                        <tr>
                            <td class="td-info">运费:</td>
                            <td class="td-price">¥0.00</td>
                        </tr>
                        <tr>
                            <td class="td-info">优惠券:</td>
                            <td class="td-price">¥-0.00</td>
                        </tr>
                    </table>
                </div>
                <div class="order_price_bottom">
                    <span>实付:</span>&emsp;&emsp;&emsp;
                    <span>¥{{number_format($parent_order["all_price"],2)}}</span>
                </div>
            </div>
        </div>

    </main>
    <script>
        var state_admin = <?=json_encode($state, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)?>;
        $(".order_state_name").on("click", function () {
            loading.show()
            var th = $(this)
            var state = parseInt(th.attr("data-state"))
            var parent_order_id = th.attr("data-order-id")
            if (parent_order_id == "" || typeof parent_order_id == "undefined") {
                return false
            }
            parent_order_id = parseInt(parent_order_id)
            switch (state) {
                case 0:
                    pay(th, parent_order_id)
                    break
                case 1:
                    alert("已通知商家")
                    break
                case 2:
                case 3:
                    add_state(th, parent_order_id, state)
                    break
                default:
                    loading.hide()
                    break
            }
        })

        function pay(jq, parent_order_id) {
            window.location.href = "/order/pay/" + parent_order_id
            loading.hide()
        }

        function add_state(th, id, state) {
            var data = {
                id: id,
                state: state,
                _token: csrf_token
            }
            $.post("/api/add_state", data, function (e) {
                if (e.errorCode == 0) {
                    window.location.reload()
                } else {
                    alert(e.errorMsg);
                }
                loading.hide()
            });
        }

        function change_state_text(th, state) {
            th.attr("data-state", state)
            th.text(state_admin[state])
        }

        $(".close_order").on("click", function () {
            loading.show()
            var th = $(this)
            var parent_order_id = parseInt(th.attr("data-order-id"))
            var state = parseInt(th.attr("data-state"))
            if (state == 6)
                delete_order(th, parent_order_id)
            else
                close_order(th, parent_order_id)
        })

        function close_order(th, id) {
            var data = {
                id: id,
                _token: csrf_token
            }
            $.post("/api/close_order", data, function (e) {
                if (e.errorCode == 0) {
                    alert("订单关闭成功");
                    window.location.reload()
                } else {
                    alert(e.errorMsg);
                }
                loading.hide()
            });
        }

        function delete_order(th, id) {
            var data = {
                id: id,
                _token: csrf_token
            }
            $.post("/api/delete_order", data, function (e) {
                if (e.errorCode == 0) {
                    alert("订单删除成功");
                    window.location.href = "/user/order/list"
                } else {
                    alert(e.errorMsg);
                }
                loading.hide()
            });
        }
    </script>
@endsection